﻿using System;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace ScoutNET.Helpers
{
    public static class PagerLinksHelper
    {
        public static MvcHtmlString PagerLinks(this HtmlHelper htmlHelper, int pageIndex, int pageCount, string actionName, object routeValues)
        {
            if (string.IsNullOrEmpty(actionName))
            {
                throw new ArgumentException("The argument must have a value", "actionName");
            }

            RouteValueDictionary rvd = new RouteValueDictionary(routeValues);
            if (!rvd.ContainsKey("controller"))
            {
                throw new Exception("routeValues must contains a key controller");
            }
            rvd.Add("page", 0);
                
            StringBuilder builder = new StringBuilder();

            if (pageCount > 1)
            {

                builder.Append("<div id=\"paginator\">");

                if (pageIndex != 1)
                {
                    if (pageCount > 2)
                    {
                        // first page link
                        rvd["Page"] = 1;
                        builder.Append(htmlHelper.ActionLink(Resources.ResourceManager.GetString("PagerHelper_FirstLink"), actionName, new { page = rvd["Page"] }));
                    }

                    if (pageIndex > 1)
                    {
                        // previous page link
                        int previousIndex = pageIndex - 1;
                        rvd["Page"] = previousIndex;
                        builder.Append(htmlHelper.ActionLink(Resources.ResourceManager.GetString("PagerHelper_PreviousLink"), actionName, new { page = rvd["Page"] }, new { @class = "prev", rel = "prev" }).ToHtmlString());
                    }
                    else
                    {
                        builder.Append(string.Format(@"<span class=""link-off"">{0}</span>", Resources.ResourceManager.GetString("PagerHelper_PreviousLink")));
                    }
                }
                else
                {
                    if (pageCount > 2)
                    {
                        builder.Append(string.Format(@"<span class=""link-off"">{0}</span>", Resources.ResourceManager.GetString("PagerHelper_FirstLink")));
                    }
                    builder.Append(string.Format(@"<span class=""link-off"">{0}</span>", Resources.ResourceManager.GetString("PagerHelper_PreviousLink")));
                }

                builder.Append(" - ");

                // calc low and high limits for numeric links
                int intLow = pageIndex - 2;
                int intHigh = pageIndex + 2;
                if (intLow < 1) intLow = 1;
                if (intHigh > pageCount) intHigh = pageCount;
                if (intHigh - intLow < 5) while ((intHigh < intLow + 4) && intHigh < pageCount) intHigh++;
                if (intHigh - intLow < 5) while ((intLow > intHigh - 4) && intLow > 1) intLow--;
                for (int x = intLow; x < intHigh + 1; x++)
                {
                    // numeric links
                    if (x == pageIndex)
                    {
                        builder.Append(String.Format("<span class=\"this-page\">{0}</span>", x));
                    }
                    else
                    {
                        rvd["Page"] = x;
                        builder.Append(htmlHelper.ActionLink(x.ToString(), actionName, new { page = rvd["Page"] }));
                    }
                }

                builder.Append(" - ");

                if (pageIndex != pageCount)
                {

                    if (pageIndex < pageCount)
                    {
                        // next page link
                        int nextIndex = pageIndex + 1;
                        rvd["Page"] = nextIndex;
                        builder.Append(htmlHelper.ActionLink(Resources.ResourceManager.GetString("PagerHelper_NextLink"), actionName, new { page = rvd["Page"] }, new { @class = "next", rel = "next" }));
                    }
                    else
                    {
                        builder.Append(String.Format(@"<span class=""link-off"">{0}</span>", Resources.ResourceManager.GetString("PagerHelper_NextLink")));
                    }

                    if (pageCount > 2)
                    {
                        // last page link
                        rvd["Page"] = pageCount;
                        builder.Append(htmlHelper.ActionLink(Resources.ResourceManager.GetString("PagerHelper_LastLink"), actionName, new { page = rvd["Page"] }));
                    }
                }
                else
                {
                    builder.Append(String.Format(@"<span class=""link-off"">{0}</span>", Resources.ResourceManager.GetString("PagerHelper_NextLink")));
                    if (pageCount > 2)
                    {
                        builder.Append(String.Format(@"<span class=""link-off"">{0}</span>", Resources.ResourceManager.GetString("PagerHelper_LastLink")));
                    }
                }
                builder.Append("</div>");
            }
            return MvcHtmlString.Create(builder.ToString());
        }
    }
}